package com.ato.Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author wangjing
 * @create 2021--09--25--20:53
 */
public class ThreadCreateMethodsThree {
    public static void main(String[] args) {

        Three t3 = new Three();

        FutureTask<Integer> futureTask = new FutureTask<Integer>(t3);

        new Thread(futureTask).start();
        try { // 优势2：可以抛出异常，因此可以值main()主线程try
            Integer integer = futureTask.get();// 优势：1：可以有返回值  需要借助 FutureTask类
            System.out.println(integer);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}

class Three implements Callable {
    @Override
    public Object call() throws Exception {
        int total = 0;
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                total += i;
            }
        }
        return total;
    }
}